﻿using System;
using Results.Models;

namespace Results.ViewModels
{
    /// <summary>
    /// The total points (ranking) for a given player
    /// </summary>
    public class RankingViewModel : ViewModelBase
    {
        /// <summary>
        /// The command to invoke when this player is selected
        /// </summary>
        public RelayCommand PlayerSelected { get; private set; }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="playerModel">Source data</param>
        /// <param name="points">Points that this player earned</param>
        /// <param name="notifyPlayerSelected">Methods to call if the user selects something</param>
        public RankingViewModel(PlayerModel playerModel, int points, INotifySelection notifyPlayerSelected)
        {
            _playerModel = playerModel;

            Name = _playerModel.LastName + ", " + _playerModel.FirstName;
            Points = points.ToString();

            PlayerSelected = new RelayCommand(p => OnPlayerSelected());
            _notifyPlayerSelected = notifyPlayerSelected;
        }

        String _name;
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    Notify("Name");
                }
            }
        }

        String _points;
        public String Points
        {
            get
            {
                return _points;
            }
            set
            {
                if (_points != value)
                {
                    _points = value;
                    Notify("Points");
                }
            }
        }

        void OnPlayerSelected()
        {
            _notifyPlayerSelected.PlayerSelected(_playerModel);
        }

        readonly INotifySelection _notifyPlayerSelected;
        readonly PlayerModel _playerModel;
    }
}
